package com.glb.gilibuy.service.impl;

import com.glb.gilibuy.dao.ProductCategoryDao;
import com.glb.gilibuy.entity.ProductCategory;
import com.glb.gilibuy.entity.ProductCategoryExample;
import com.glb.gilibuy.entity.UserOrderItem;
import com.glb.gilibuy.service.ProductCategoryService;
import com.glb.gilibuy.util.BeanUtil;
import com.glb.gilibuy.web.vo.ProductCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/4 10:56
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryDao productCategoryDao;

    @Override
    public ProductCategory selectCategoryByCategoryId(String categoryId) {
        if (!"".equals(categoryId)) {
            ProductCategoryExample productCategoryExample = new ProductCategoryExample();
            productCategoryExample.or().andProductCategoryIdEqualTo(categoryId).andIsDeletedEqualTo(0);
            List<ProductCategory> productCategoryList = productCategoryDao.selectByExample(productCategoryExample);
            if (productCategoryList.isEmpty()) {
                return null;
            }
            return productCategoryList.get(0);
        }
        return null;
    }

    @Override
    public List<ProductCategoryVO> selectAllCategories() {
        //先找到所有无父节点的一级分类
        List<ProductCategory> productCategoryList = selectCategoriesByParentId("0");

        if (!productCategoryList.isEmpty()) {

            List<ProductCategoryVO> productCategoryVOList = BeanUtil.copyList(productCategoryList, ProductCategoryVO.class);

            //再找一级分类的子分类
            int i = 0;
            for (ProductCategoryVO productCategoryVO : productCategoryVOList) {
                i++;
                List<ProductCategoryVO> childList = selectCategoryChild(productCategoryVO);
                productCategoryVO.setChildList(childList);
            }
            System.out.println("外层循环了" + i + "次");
            return productCategoryVOList;
        }

        return null;
    }

    @Override
    public int addCategory(ProductCategory productCategory) {
        if (productCategory == null) {
            return 0;
        }

        //后台不校参

        if (productCategoryDao.insertSelective(productCategory) > 0) {
            return 1;
        }
        return 0;
    }

    //根据父分类id找分类列表
    private List<ProductCategory> selectCategoriesByParentId(String parentId) {
        if (!"".equals(parentId)) {
            ProductCategoryExample productCategoryExample = new ProductCategoryExample();
            productCategoryExample.setOrderByClause("product_category_sequence desc , id desc");
            productCategoryExample.or().andParentCategoryIdEqualTo(parentId).andIsDeletedEqualTo(0);
            List<ProductCategory> productCategoryList = productCategoryDao.selectByExample(productCategoryExample);
            return productCategoryList;
        }
        return null;
    }

    //找子分类
    private List<ProductCategoryVO> selectCategoryChild(ProductCategoryVO productCategoryVO) {

        List<ProductCategory> productCategoryList = selectCategoriesByParentId(productCategoryVO.getProductCategoryId());
        List<ProductCategoryVO> productCategoryVOList = BeanUtil.copyList(productCategoryList, ProductCategoryVO.class);

        for (ProductCategoryVO productCategoryVO2 : productCategoryVOList) {
            List<ProductCategoryVO> childList = selectCategoryChild(productCategoryVO2);
            productCategoryVO2.setChildList(childList);
        }
        return productCategoryVOList;
    }
}
